﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.Collections;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace CRMHelpers
{
    public class LabelHelper
    {
        private IOrganizationService CrmContext;

        private int _languageCode;

        private int _defaultLanguage = 1033;



        public LabelHelper()
        {
            throw new Exception("Not Implemented");
        }

        public LabelHelper(int Language, IOrganizationService service)
        {
   
            CrmContext = service;
            _languageCode = Language;
        }

        public int CurrentLanguageCode
        {
            get { return _languageCode; }
        }


        


        public String GetLabelOfField(String entityName, String fieldName)
        {
            return LabelToString(((RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest { EntityLogicalName = entityName, LogicalName = fieldName })).AttributeMetadata.DisplayName);
        }


        public String GetValueofOptionorPicklistInDefaultlanguage(String entityLogicalname, String fieldName, int value)
        {

            var Field = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = entityLogicalname,
                LogicalName = fieldName

            });

            PicklistAttributeMetadata pickListOptionField = (PicklistAttributeMetadata)Field.AttributeMetadata;
            return GetValueofOptionInDefaultlanguage(pickListOptionField.OptionSet.Options, value);
        }


        protected String LabelToString(Microsoft.Xrm.Sdk.Label l)
        {
            String result = "";

            result = LabelTranslate(l.LocalizedLabels).FirstOrDefault<LocalizedLabel>(c => c.LanguageCode == CurrentLanguageCode).Label;

            return result;
        }

        protected LocalizedLabelCollection LabelTranslate(LocalizedLabelCollection listelabel)
        {
            if (listelabel.Count<LocalizedLabel>(l => l.LanguageCode == CurrentLanguageCode) == 0)
            {
                LocalizedLabel ll = new LocalizedLabel(listelabel.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label, CurrentLanguageCode);
                listelabel.Add(ll);
            }

            return listelabel;
        }

        protected String GetValueofOptionInDefaultlanguage(OptionMetadataCollection ol, int value)
        {
            String result = "";

            foreach (OptionMetadata po in ol)
            {
                if (po.Value == value)
                {

                    result = po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label;


                }

            }

            return result;
        }

        public int GetValueofBooleanLabelinUserorDefaultLanguage(BooleanAttributeMetadata boolOption, String _value)
        {
            int result = -1;



            OptionMetadataCollection pickListOption = new OptionMetadataCollection { boolOption.OptionSet.TrueOption, boolOption.OptionSet.FalseOption };

            foreach (OptionMetadata po in pickListOption)
            {



                if (po.Label.UserLocalizedLabel != null && _value.Equals(po.Label.UserLocalizedLabel.Label))
                {
                    return po.Value.Value;
                }
                else
                {
                    if (_value.Equals(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label))
                        return po.Value.Value;
                }


            }

            return result;
        }

        public int GetValueofPicklistLabelinUserorDefaultLanguage(OptionMetadataCollection pickListOption, String _value)
        {
            int result = 0;

            



            foreach (OptionMetadata po in pickListOption)
            {



                if (po.Label.UserLocalizedLabel != null && _value.Equals(po.Label.UserLocalizedLabel.Label))
                {
                    return po.Value.Value;
                }
                else
                {
                    if (_value.Equals(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label))
                        return po.Value.Value;
                }


            }

            return result;
        }

        public int GetValueofPicklistLabelinUserorDefaultLanguage(String _entityLogicalName, String _columnname, String _value)
        {
            int result = 0;

            var response = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = _entityLogicalName,
                LogicalName = _columnname

            });



            PicklistAttributeMetadata pickListOption = (PicklistAttributeMetadata)response.AttributeMetadata;



            foreach (OptionMetadata po in pickListOption.OptionSet.Options)
            {



                    if (po.Label.UserLocalizedLabel != null && _value.Equals(po.Label.UserLocalizedLabel.Label))
                        result = po.Value.Value;
                    else
                    {
                        if (_value.Equals(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label))
                            result = po.Value.Value;
                    }
                

            }

            return result;
        }

        public int GetValueofStatusLabelinUserorDefaultLanguage(String _entityLogicalName, String _columnname, String _value)
        {
            int result = 0;

            var response = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = _entityLogicalName,
                LogicalName = _columnname

            });



            StatusAttributeMetadata pickListOption = (StatusAttributeMetadata)response.AttributeMetadata;



            foreach (OptionMetadata po in pickListOption.OptionSet.Options)
            {
                if (po.Label.UserLocalizedLabel != null && _value.Equals(po.Label.UserLocalizedLabel.Label))
                    result = po.Value.Value;
                else
                {
                    if (_value.Equals(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label))
                        result = po.Value.Value;
                }

            }

            return result;
        }




        public int GetValueofStateLabelinUserorDefaultLanguage(String _entityLogicalName, String _columnname, String _value)
        {
            int result = 0;

            var response = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = _entityLogicalName,
                LogicalName = _columnname

            });



            StateAttributeMetadata pickListOption = (StateAttributeMetadata)response.AttributeMetadata;



            foreach (OptionMetadata po in pickListOption.OptionSet.Options)
            {
                if (po.Label.UserLocalizedLabel != null && _value.Equals(po.Label.UserLocalizedLabel.Label))
                    result = po.Value.Value;
                else
                {
                    if (_value.Equals(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label))
                        result = po.Value.Value;
                }

            }

            return result;
        }

        public String GetValueofState(String entityLogicalname, String fieldName, int value)
        {

            var Field = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = entityLogicalname,
                LogicalName = fieldName

            });

            StatusAttributeMetadata pickListOptionField = (StatusAttributeMetadata)Field.AttributeMetadata;
            return GetValueofOption(pickListOptionField.OptionSet.Options, value);
        }

        public String GetValueofStateInDefaultLanguage(String entityLogicalname, String fieldName, int value)
        {

            var Field = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = entityLogicalname,
                LogicalName = fieldName

            });

            StateAttributeMetadata pickListOptionField = (StateAttributeMetadata)Field.AttributeMetadata;
            return GetValueofOptionInDefaultlanguage(pickListOptionField.OptionSet.Options, value);
        }
        public String GetValueofStatusInDefaultLanguage(String entityLogicalname, String fieldName, int value)
        {

            var Field = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = entityLogicalname,
                LogicalName = fieldName

            });

            StatusAttributeMetadata pickListOptionField = (StatusAttributeMetadata)Field.AttributeMetadata;
            return GetValueofOptionInDefaultlanguage(pickListOptionField.OptionSet.Options, value);
        }

        public String GetStringValueofOptionorPicklistInDefaultLanguage(String entityLogicalname, String fieldName, int value)
        {

            var Field = (RetrieveAttributeResponse)CrmContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = entityLogicalname,
                LogicalName = fieldName

            });

            PicklistAttributeMetadata pickListOptionField = (PicklistAttributeMetadata)Field.AttributeMetadata;
            return GetValueofOptionInDefaultlanguage(pickListOptionField.OptionSet.Options, value);
        }

        protected String GetValueofOption(OptionMetadataCollection ol, int value)
        {
            String result = "";

            foreach (OptionMetadata po in ol)
            {
                if (po.Value == value)
                {

                    if (po.Label.LocalizedLabels.Count<LocalizedLabel>(l => l.LanguageCode == CurrentLanguageCode) == 0)
                    {
                        LocalizedLabel ll = new LocalizedLabel(po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == _defaultLanguage).Label, CurrentLanguageCode);
                        result = ll.Label;
                        po.Label.LocalizedLabels.Add(ll);
                    }
                    else
                    {
                        result = po.Label.LocalizedLabels.FirstOrDefault<LocalizedLabel>(l => l.LanguageCode == CurrentLanguageCode).Label;
                    }

                }

            }

            return result;
        }

    }
}
